﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.Common;
using System.Text.RegularExpressions;

namespace ver2vhd
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //提供对话框来打开文件
        public int openFile()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = "D://";
            openFile.Filter = "Verilog Flile(*.v)|*.v";
           // openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.Cancel) return -1;
            var filePath = openFile.FileName;
           
            string fileType = System.IO.Path.GetExtension(filePath);
            if (string.IsNullOrEmpty(fileType)) return -2;
            FileInfo fileinfo = new FileInfo(openFile.SafeFileName);
            //在此处添加后续操作
            List<string> list_str = new List<string>();
            list_str = loadFile(filePath);
            getModuleName(list_str);
            return 0;
        
        }
        //将目标文件按行装载在list中
        public List<string> loadFile(string filePath)
        {
            List<string> list_str = new List<string>();
            FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            StreamReader sr = new StreamReader(fs, Encoding.Default);

            //记录每次读取的一行记录
            string strLine = "";
            while ((strLine = sr.ReadLine()) != null)
            {
                list_str.Add(strLine);
            }
            return list_str;
        
        }
        public string getModuleName(List<string> list_str)
        { 
            //返回值定义
            string moduleName = "err";

            foreach (string ver_line in list_str)
            {
                //字符预处理
                string ver_lineRegu;
                ver_lineRegu = ver_line.Replace(" ", ""); //删除空格
                //module 和module name需要在同一行，符合一般习惯，其余情况暂不考虑
                if (ver_lineRegu.IndexOf("module") == 0)
                {
                    if (ver_lineRegu.Contains("("))
                    {
                        moduleName = ver_lineRegu.Substring(6, ver_lineRegu.IndexOf("(") - 6);
                        break;

                    }
                    else
                    {
                        moduleName = ver_lineRegu.Substring(6);
                        break;
                    }
                
                }
            
            
            }



            return moduleName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFile();
        }
        
    }
}
